<template>
	<view>



	</view>
</template>

<script>
	import {
		mapState
	} from 'vuex';
	export default {
		data() {
			return {
				loading: false,
				goodsindex: 0,
				pageNum: 1,
				passnum: 0,
				msgType: 'success',
				statusnum: 1,
				datas: {
					user_id: '',
					goods: [],
					directUpload: 1,
					type: 1
				},
				goodsdata: [],
				goodsindex: 0,
				teststr: [],
				isModalOpen: false,
				testurl: [],
				valnum: 0,
				timerId: null
			};
		},
		computed: {
			...mapState(['data'])
		},
		onLoad(options) {
			this.createWebviewAndLoadURL(options.status);
			console.log(options.status)
		},
		methods: {
			startTimer(wv) {
				this.timerId = setInterval(() => {
					let url = wv.getURL()
					console.log(url)
					if (url != null) {
						if (url.includes("h5.m.taobao.com/mlapp/mytaobao.html") || url.includes(
								"m.taobao.com/mytaobao/index.html") || url.includes(
								"h5.m.taobao.com/index_error.html") || url.includes(
								"main.m.taobao.com/index.html")) {
							this.datas.goods = []
							this.valnum = 0
							this.pageNum = 1

							// wv.loadURL(" ")
							clearInterval(this.timerId);
							console.log(11111)
							// this.timerId= null
							// wv.loadURL(" ")

							setTimeout(() => {
								let url =
									`https://buyertrade.taobao.com/trade/itemlist/list_bought_items.htm?action=itemlist/BoughtQueryAction&event_submit_do_query=1&tabCode=waitConfirm&pageNum=${this.pageNum}`
								console.log(url)
								console.log(this.statusnum)
								if (this.statusnum == 1) {
									this.statusnum += 1
									this.createWebviewAndLoadURL3(url)
								}


							}, 600);

						} 
						// else if (url.includes(" login.m.taobao.com/login.htm")) {
						// 	this.datas.goods = []
						// 	this.valnum = 0
						// 	this.pageNum = 1

						// 	wv.loadURL("")
						// 	clearInterval(this.timerId);

						// 	setTimeout(() => {
						// 		let url = `https://login.m.taobao.com/login.htm`
						// 		this.createWebviewAndLoadURL3(url)

						// 	}, 600);

						// }
					} else {
						clearInterval(this.timerId);
						// timerId = null
					}


				}, 500);
			},

			createWebviewAndLoadURL(status) {
				// 在 APP-PLUS 平台下执行以下代码
				// #ifdef APP-PLUS
				let loadCount = 0;
				const wv = plus.webview.create("", "custom-webview", {
					plusrequire: "none",
					'uni-app': 'none',
					top: uni.getSystemInfoSync().statusBarHeight + 44,
					bottom: 0
				});

				this.handleWebViewLoaded(wv)



				wv.loadURL("https://login.m.taobao.com/login.htm");
				// 100
				setTimeout(function() {
					wv.loadURL("https://login.m.taobao.com/login.htm");
				}, 100);
				if (status == "0") {
					setTimeout(() => {
						let url = 'https://buyertrade.taobao.com/trade/itemlist/list_bought_items.htm?action=itemlist/BoughtQueryAction&event_submit_do_query=1&tabCode=waitConfirm&pageNum=1'
						uni.request({
							url: url,
							method: 'GET',
							success: res => {
								let rawData = JSON.stringify(res)
								const regex = /JSON\.parse(.*?)\;\\n\<\/script/;
								const match = rawData.match(regex);
								if (match) {
									// wv.loadURL(url);
									let url =
										`https://buyertrade.taobao.com/trade/itemlist/list_bought_items.htm?action=itemlist/BoughtQueryAction&event_submit_do_query=1&tabCode=waitConfirm&pageNum=${this.pageNum}`
										this.datas.goods = []
										this.valnum = 0
										this.pageNum = 1
										this.createWebviewAndLoadURL3(url)
									
								} else {
									console.log("未找到匹配的内容")
								}
							},
							fail: err => {
								console.error('请求失败：', err);
								uni.hideLoading();
								uni.setStorageSync('taobaotesthtml', res.data);
								uni.navigateTo({
									url: '/pages/taobaoimport/taobaotest'
								});
								this.loading = false
							}
						});
					}, 1500);
				}
				
				




				const currentWebview = this.$scope.$getAppWebview();
				currentWebview.append(wv);

				// 延时一秒后获取 Webview 样式
				setTimeout(function() {
					console.log(wv.getStyle());
				}, 1000);

				function unescapeString(str) {
					return str.replace(/\\n/g, '\n')
						.replace(/\\r/g, '\r')
						.replace(/\\t/g, '\t')
						.replace(/\\b/g, '\b')
						.replace(/\\f/g, '\f')
						.replace(/\\"/g, '"')
						.replace(/\\\\/g, '\\');
				}


				// #endif

			},

			createWebviewAndLoadURL2(url) {
				// 在 APP-PLUS 平台下执行以下代码
				// #ifdef APP-PLUS
				let loadCount = 0;
				const wv = plus.webview.create("", "custom-webview", {
					plusrequire: "none",
					'uni-app': 'none',
					top: uni.getSystemInfoSync().statusBarHeight + 44,
					bottom: 0
				});

				this.handleWebViewLoaded(wv)



				// wv.loadURL(url);
				clearInterval(this.timerId);
				setTimeout(() => {
					this.startTimer(wv)
				}, 1000);
				setTimeout(function() {
					wv.loadURL(url);
				}, 100);
				const currentWebview = this.$scope.$getAppWebview();
				currentWebview.append(wv);

				// 延时一秒后获取 Webview 样式
				setTimeout(function() {
					console.log(wv.getStyle());
				}, 1000);



				function unescapeString(str) {
					return str.replace(/\\n/g, '\n')
						.replace(/\\r/g, '\r')
						.replace(/\\t/g, '\t')
						.replace(/\\b/g, '\b')
						.replace(/\\f/g, '\f')
						.replace(/\\"/g, '"')
						.replace(/\\\\/g, '\\');
				}


				// #endif

			},

			createWebviewAndLoadURL3(url) {
				// 在 APP-PLUS 平台下执行以下代码
				// #ifdef APP-PLUS

				let loadCount = 0;
				const wv = plus.webview.create("", "custom-webview", {
					plusrequire: "none",
					'uni-app': 'none',
					top: uni.getSystemInfoSync().statusBarHeight + 44,
					bottom: 0
				});

				this.handleWebViewLoaded(wv)


				setTimeout(() => {
					wv.loadURL('about:blank');
					wv.setStyle({
					    backgroundColor: '#FFFFFF'
					});
					setTimeout(() => {
						console.log(url)
						wv.loadURL(url);
					}, 100);
					// wv.loadURL(url);
				}, 100);
				const currentWebview = this.$scope.$getAppWebview();
				currentWebview.append(wv);

				// 延时一秒后获取 Webview 样式
				setTimeout(function() {
					console.log(wv.getStyle());
				}, 1000);



				function unescapeString(str) {
					return str.replace(/\\n/g, '\n')
						.replace(/\\r/g, '\r')
						.replace(/\\t/g, '\t')
						.replace(/\\b/g, '\b')
						.replace(/\\f/g, '\f')
						.replace(/\\"/g, '"')
						.replace(/\\\\/g, '\\');
				}


				// #endif

			},

			decodeString(str) {
				var result = '';
				for (var i = 0; i < str.length; i++) {
					if (str[i] === 'u' && i + 4 < str.length) {
						var hexCode = str.substr(i + 1, 4);
						result += String.fromCharCode(parseInt(hexCode, 16));
						i += 4;
					} else {
						result += str[i];
					}
				}
				return result;
			},
			strdemctrion3(rules1, rules2, stritem) {
				var pattern = new RegExp(rules1 + "(.*?)" + rules2);
				var match = stritem.match(pattern);
				if (match) {
					return match[1]; // 返回匹配到的内容
				} else {
					return "未找到匹配的结果";
				}
			},
			fordatagoods() {
				let copydata = []
				let datat = JSON.parse(JSON.stringify(this.datas))

				for (var i = 0; i < datat.goods.length; i++) {
					let item = datat.goods[i]
					let goodname = item.good_name
					for (var j = 0; j < goodname.length; j++) {
						let a = {
							order_sn: item.order_sn, //物品单号
							sellerid: item.sellerid,
							good_name: item.good_name[j], //物品名称
							good_price: item.good_price[j], //物品原来价格
							unit_price: item.unit_price[j], //物品实付价格
							good_img: item.good_img[j], //物品图片
							logisticscompany: item.good_name.length == item.logistics_sn.length ? item
								.logisticscompany[j] : item.logisticscompany[0], //物流公司
							logistics_sn: item.good_name.length == item.logistics_sn.length ? item.logistics_sn[j] :
								item.logistics_sn[0], //物流单号
							good_Items: item.good_Items[j], //物品品名
							num: item.num[j], //物品数量
							checked: false
						}
						copydata.push(a)
					}
				}

				return copydata
			},

			handleWebViewLoaded(wv) {
				wv.addEventListener("loaded", () => {
					wv.overrideUrlLoading({
						mode: 'reject'
					}, (e) => {
						// uni.hideLoading();
						console.log('拦截请求：' + e.url);




						if (e.url.includes("h5.m.taobao.com/mlapp/mytaobao.html") || e.url.includes(
								"m.taobao.com/mytaobao/index.html")) {
							if (this.timerId != null) {
								clearInterval(this.timerId);
							}

							this.datas.goods = []
							this.valnum = 0
							this.pageNum = 1
							wv.loadURL(
								`https://buyertrade.taobao.com/trade/itemlist/list_bought_items.htm?action=itemlist/BoughtQueryAction&event_submit_do_query=1&tabCode=waitConfirm&pageNum=${this.pageNum}`
							);
						} else if (e.url.includes("detail.i56.taobao.com/trace/trace_detail.htm")) {
							// console.log('拦截请求：' + e.url);
							uni.request({
								url: e.url,
								method: e.method,
								success: res => {
									if (this.timerId != null) {
										clearInterval(this.timerId);
									}
									// console.log(res.data)
									let resstr = res.data
									let mailNoreg = /var mailNo = \"(.*?)\"/
									let cpNamreg = /var cpName = \"(.*?)\"/
									let mailNomatch = resstr.match(mailNoreg);
									let cpNammatch = resstr.match(cpNamreg);
									if (mailNomatch && mailNomatch[1] != "") {
										this.goodsdata[this.goodsindex].logistics_sn =
											mailNomatch[1]
									}
									if (cpNammatch && cpNammatch[1] != "") {
										this.goodsdata[this.goodsindex].logisticscompany =
											cpNammatch[1]
									}
									if (this.goodsindex + 1 < this.goodsdata.length) {
										this.goodsindex += 1
										let goods = this.goodsdata
										for (var i = this.goodsindex; i < goods.length; i++) {
											this.goodsindex = i
											let item = goods[i]
											if (item.logistics_sn == '0000') {
												let url =
													`https://detail.i56.taobao.com/trace/trace_detail.htm?tId=${item.order_sn}&userId=${item.sellerid}`
												wv.loadURL(url);
												return
											}

											if (this.goodsindex + 1 == goods
												.length) {
												uni.hideLoading();
												this.datas.goods = goods
												this.valnum = 0
												uni.setStorageSync(
													'taobaoparamsdata', JSON
													.stringify(this.datas));
												uni.navigateTo({
													url: '/pages/taobaoimport/taobaogoods'
												});
											}
										}
									} else {
										uni.hideLoading();
										this.datas.goods = goods
										this.valnum = 0
										uni.setStorageSync(
											'taobaoparamsdata', JSON
											.stringify(this.datas));
										uni.navigateTo({
											url: '/pages/taobaoimport/taobaogoods'
										});
									}
								},
								fail: err => {
									console.error('请求失败：', err);
								}
							});
						} else if (e.url.includes("trade.tmall.com/detail/orderDetail.htm") || e.url
							.includes(
								"buyertrade.taobao.com/trade/detail/trade_item_detail.htm")) {
							uni.request({
								url: e.url,
								method: e.method,
								success: res => {
									if (this.timerId != null) {
										clearInterval(this.timerId);
									}
									uni.showLoading({
										title: `获取订单信息中 ${this.goodsindex + 1}/${this.datas.goods.length}`
									});
									let rawData = JSON.stringify(res)
									let trimmedString = rawData.replace(/\s/g, '');
									const regex = /vardetailData=(.*?)\\n<\/script>/;
									const match = trimmedString.match(regex);
									if (match) {
										const jsonString = match[1];
										let jsonString2 = jsonString.replace(/'/g, '');
										var stringWithoutEscape = jsonString2.replace(/\\/g,
											'');
										// console.log(stringWithoutEscape)
										let jsonstringWithoutEscape = JSON.parse(
											stringWithoutEscape)

										let prompt = jsonstringWithoutEscape.overStatus.prompt
										if (prompt && prompt.length > 0) {
											for (var i = 0; i < prompt.length; i++) {
												let item = prompt[i]
												if (item.key == '物流' || item.key == '配送时间') {
													this.datas.goods[this.goodsindex]
														.logisticscompany[0] = item.content[0]
														.mailNo ? item.content[0]
														.companyName : item.content[0]
														.companyName
													this.datas.goods[this.goodsindex]
														.logistics_sn[0] = item.content[0]
														.mailNo ? item.content[0]
														.mailNo : item.content[1]
														.mailNo
												}
											}

										} else {
											let list = jsonstringWithoutEscape.orders
												.list
											let goodnames = this.datas.goods[this.goodsindex]
												.good_name
											if (list && list.length > 0) {
												for (var i = 0; i < list.length; i++) {
													let logistic = list[i].logistic
													let status = list[i].status
													let title = status[0].subOrders[0].itemInfo
														.title
													for (var p = 0; p < goodnames
														.length; p++) {
														let itemgood = goodnames[p]
														if (itemgood == title) {
															this.datas.goods[this.goodsindex]
																.logisticscompany[p] = logistic
																.content[0].companyName
															this.datas.goods[this.goodsindex]
																.logistics_sn[p] = logistic
																.content[0].mailNo
														}
													}
												}

											}
										}


										if (this.goodsindex + 1 < this.datas.goods.length) {
											this.goodsindex += 1
											let url = this.datas.goods[this.goodsindex]
												.gooddetailurl
											wv.loadURL(url);
										} else {
											uni.showLoading({
												title: `订单信息校验中...`
											});
											let goods = this.fordatagoods()
											this.goodsdata = goods

											if (goods.length > 0) {
												for (var i = 0; i < goods.length; i++) {
													this.goodsindex = i
													let item = goods[i]
													if (item.logistics_sn == '0000') {
														let url =
															`https://detail.i56.taobao.com/trace/trace_detail.htm?tId=${item.order_sn}&userId=${item.sellerid}`
														wv.loadURL(url);
														return
													}

													if (this.goodsindex + 1 == goods
														.length) {
														uni.hideLoading();
														this.datas.goods = goods
														this.valnum = 0
														uni.setStorageSync(
															'taobaoparamsdata', JSON
															.stringify(this.datas));
														uni.navigateTo({
															url: '/pages/taobaoimport/taobaogoods'
														});
													}
												}

											} else {
												uni.hideLoading();
												this.datas.goods = goods
												this.valnum = 0
												uni.setStorageSync('taobaoparamsdata', JSON
													.stringify(this.datas));
												uni.navigateTo({
													url: '/pages/taobaoimport/taobaogoods'
												});
											}
										}
									} else {
										// console.log("未找到匹配的内容")
										const regex2 = /vardata=JSON.parse\(\'(.*?)\'\);/;
										const match2 = trimmedString.match(regex2);

										if (match2) {

											const jsonString = match2[1];
											var stringWithoutEscape = jsonString.replace(
												/\\\\\\\\\\\"|\\\\"/g,
												function(matched) {
													return matched === '\\\\\\\\\\\"' ?
														'\\\"' : '\"';
												});
											var stringWithoutEscape2 = stringWithoutEscape
												.replace(/(?!\\u|\\\/|\\\\\")\\/g, '');
											let jsonString2 = JSON.parse(stringWithoutEscape2)

											if (jsonString2.logisticsName) {
												this.datas.goods[this.goodsindex]
													.logisticscompany[0] = jsonString2
													.logisticsName
												this.datas.goods[this.goodsindex]
													.logistics_sn[0] = jsonString2.logisticsNum
											} else if (jsonString2.deliveryInfo) {
												this.datas.goods[this.goodsindex]
													.logisticscompany[0] = jsonString2
													.deliveryInfo.logisticsName
												this.datas.goods[this.goodsindex]
													.logistics_sn[0] = jsonString2.deliveryInfo
													.logisticsNum
											}


											if (this.goodsindex + 1 < this.datas.goods
												.length) {
												this.goodsindex += 1
												let url = this.datas.goods[this.goodsindex]
													.gooddetailurl
												wv.loadURL(url);
											} else {
												let goods = this.fordatagoods()
												this.goodsdata = goods

												if (goods.length > 0) {
													for (var i = 0; i < goods.length; i++) {
														this.goodsindex = i
														let item = goods[i]
														if (item.logistics_sn == '0000') {
															let url =
																`https://detail.i56.taobao.com/trace/trace_detail.htm?tId=${item.order_sn}&userId=${item.sellerid}`
															wv.loadURL(url);
															return
														}

														if (this.goodsindex + 1 == goods
															.length) {
															uni.hideLoading();
															this.valnum = 0
															this.datas.goods = goods
															uni.setStorageSync(
																'taobaoparamsdata', JSON
																.stringify(this.datas));
															uni.navigateTo({
																url: '/pages/taobaoimport/taobaogoods'
															});
														}
													}

												} else {
													uni.hideLoading();
													this.valnum = 0
													this.datas.goods = goods
													uni.setStorageSync('taobaoparamsdata', JSON
														.stringify(this.datas));
													uni.navigateTo({
														url: '/pages/taobaoimport/taobaogoods'
													});
												}
											}
										} else {
											console.log("未找到匹配的内容")
										}


									}
								},
								fail: err => {
									console.error('请求失败：', err);
								}
							});
						} else if (e.url.includes(
								"buyertrade.taobao.com/trade/itemlist/list_bought_items.htm")) {
							uni.showLoading({
								title: '加载中，耐心等待...'
							});
							let newurl = e.url + '&order_index=80'
							uni.request({
								url: e.url,
								method: e.method,
								success: res => {
									if (this.timerId != null) {
										clearInterval(this.timerId);
									}



									let rawData = JSON.stringify(res)
									this.loading = true
									const regex = /JSON\.parse(.*?)\;\\n\<\/script/;
									const match = rawData.match(regex);
									if (match) {
										const jsonString = match[1];
										let jsonString2 = jsonString.substring(1, jsonString
											.length - 1)
										let jsonString3 = jsonString2.replace(/'/g, '');
										var stringWithoutEscape = jsonString3.replace(/\\/g,
											'');

										let jsondatas = JSON.parse(stringWithoutEscape)
										let mainOrders = jsondatas.mainOrders
										let goods = []
										// console.log(mainOrders.length)

										if (mainOrders.length > 0) {
											for (var i = 0; i < mainOrders.length; i++) {
												let item = mainOrders[i]
												let subOrders = item.subOrders
												let b = {
													order_sn: item.id, //物品单号
													sellerid: item.seller.id,
													good_name: [], //物品名称
													good_price: [], //物品原来价格
													gooddetailurl: this.operationsdata(item
														.statusInfo.operations),
													// gooddetailurl: 'https:' + item
													// 	.statusInfo
													// 	.operations[0].url == undefined ? item
													// 	.statusInfo
													// 	.operations[1].url : item
													// 	.statusInfo
													// 	.operations[0].url,
													unit_price: [], //物品实付价格
													good_img: [], //物品图片
													logisticscompany: [], //物流公司
													logistics_sn: [], //物流单号
													good_Items: [], //物品品名
													num: [], //物品数量
													checked: false
												}

												for (var j = 0; j < subOrders.length; j++) {
													let itemj = subOrders[j]
													if (itemj.id) {
														let picimg = itemj.itemInfo.pic
															.replace('80x80', '300x300');
														let skuname = itemj.itemInfo
															.skuText
															.length > 0 ? this.decodeString(
																itemj
																.itemInfo.skuText[0].name) +
															'：' + this
															.decodeString(itemj
																.itemInfo
																.skuText[0].value) : ''
														b.good_name.push(this.decodeString(
															itemj
															.itemInfo.title))
														b.good_price.push(itemj
															.priceInfo
															.original ? itemj
															.priceInfo
															.original : itemj
															.priceInfo
															.realTotal)
														b.unit_price.push(itemj
															.priceInfo
															.realTotal)
														b.good_img.push('https:' + picimg)
														b.good_Items.push(skuname)
														b.num.push(itemj.quantity)
													}
												}
												this.datas.goods.push(b)
											}
											this.datas.user_id = this.data.user.user_id
											this.pageNum += 1
											wv.loadURL(
												`https://buyertrade.taobao.com/trade/itemlist/list_bought_items.htm?action=itemlist/BoughtQueryAction&event_submit_do_query=1&tabCode=waitConfirm&pageNum=${this.pageNum}`
											);
										} else {
											if (this.datas.goods.length > 0) {
												this.pageNum = 1
												this.goodsindex = 0
												let url = this.datas.goods[this.goodsindex]
													.gooddetailurl
												// console.log(JSON.stringify(this.datas.goods))
												if (url != '') {
													wv.loadURL(url);
												} else {
													wv.loadURL(url);
												}
												// wv.loadURL(url);
											} else {
												uni.hideLoading();
												this.datas.goods = []
												this.valnum = 0
												uni.setStorageSync('taobaoparamsdata', JSON
													.stringify(this.datas));
												uni.navigateTo({
													url: '/pages/taobaoimport/taobaogoods'
												});
											}


										}





										// console.log(JSON.stringify(this.datas))
										// this.pageNum += 1

										// this.goodsindex = 0
										// let url = this.datas.goods[this.goodsindex]
										// 	.gooddetailurl
										// wv.loadURL(url);
									} else {
										console.log("未找到匹配的内容")
										uni.hideLoading();
										uni.setStorageSync('taobaotesthtml', res.data);
										uni.navigateTo({
											url: '/pages/taobaoimport/taobaotest'
										});
									}
								},
								fail: err => {
									console.error('请求失败：', err);
									uni.hideLoading();
									uni.setStorageSync('taobaotesthtml', res.data);
									uni.navigateTo({
										url: '/pages/taobaoimport/taobaotest'
									});
									this.loading = false
								}
							});
						} else {
							if (e.url.includes("pass.tmall.com/add")) {
								if (this.valnum == 0) {
									this.valnum += 1
									wv.loadURL(
										`https://buyertrade.taobao.com/trade/itemlist/list_bought_items.htm?action=itemlist/BoughtQueryAction&event_submit_do_query=1&tabCode=waitConfirm&pageNum=${this.pageNum}`
									)
								} else {
									if (!this.isModalOpen) {
										this.openModal()
									}
									clearInterval(this.timerId);
									setTimeout(() => {
										console.log('重新加载')
										this.testurl.push(e.url)
										if (this.statusnum == 1) {
											this.createWebviewAndLoadURL2(e.url)
										}
									}, 2000);
									uni.off('loaded', wv);
								}

							} else {
								if (e.url.includes('login.m.taobao.com/login.htm')) {
									
								} else {
									if (!this.isModalOpen) {
										this.openModal()
									}
									clearInterval(this.timerId);
									setTimeout(() => {
										console.log('重新加载')
										this.testurl.push(e.url)
										if (this.statusnum == 1) {
											this.createWebviewAndLoadURL2(e.url)
										}
									}, 2000);
									uni.off('loaded', wv);
								}
								
							}
						}


					});

				})
			},

			handleWebViewLoaded2(wv) {
				console.log('hand')
				wv.addEventListener("loaded", () => {
					wv.overrideUrlLoading({
						mode: 'reject'
					}, (e) => {
						// uni.hideLoading();
						console.log('拦截请求：' + e.url);

						if (!this.isModalOpen) {
							this.openModal()
						}
						clearInterval(this.timerId);
						setTimeout(() => {
							console.log('重新加载')
							this.testurl.push(e.url)
							if (this.statusnum == 1) {
								this.createWebviewAndLoadURL2(e.url)
							}

						}, 2000);
						uni.off('loaded', wv);


						// if (e.url.includes("h5.m.taobao.com/mlapp/mytaobao.html") || e.url.includes(
						// 		"m.taobao.com/mytaobao/index.html")) {
						// 	if (this.timerId != null) {
						// 		clearInterval(this.timerId);
						// 	}

						// 	this.datas.goods = []
						// 	this.valnum = 0
						// 	this.pageNum = 1
						// 	wv.loadURL(
						// 		`https://buyertrade.taobao.com/trade/itemlist/list_bought_items.htm?action=itemlist/BoughtQueryAction&event_submit_do_query=1&tabCode=waitConfirm&pageNum=${this.pageNum}`
						// 	);
						// } else 
						if (e.url.includes("detail.i56.taobao.com/trace/trace_detail.htm")) {
							// console.log('拦截请求：' + e.url);
							uni.request({
								url: e.url,
								method: e.method,
								success: res => {
									if (this.timerId != null) {
										clearInterval(this.timerId);
									}
									// console.log(res.data)
									let resstr = res.data
									let mailNoreg = /var mailNo = \"(.*?)\"/
									let cpNamreg = /var cpName = \"(.*?)\"/
									let mailNomatch = resstr.match(mailNoreg);
									let cpNammatch = resstr.match(cpNamreg);
									if (mailNomatch && mailNomatch[1] != "") {
										this.goodsdata[this.goodsindex].logistics_sn =
											mailNomatch[1]
									}
									if (cpNammatch && cpNammatch[1] != "") {
										this.goodsdata[this.goodsindex].logisticscompany =
											cpNammatch[1]
									}
									if (this.goodsindex + 1 < this.goodsdata.length) {
										this.goodsindex += 1
										let goods = this.goodsdata
										for (var i = this.goodsindex; i < goods.length; i++) {
											this.goodsindex = i
											let item = goods[i]
											if (item.logistics_sn == '0000') {
												let url =
													`https://detail.i56.taobao.com/trace/trace_detail.htm?tId=${item.order_sn}&userId=${item.sellerid}`
												wv.loadURL(url);
												return
											}

											if (this.goodsindex + 1 == goods
												.length) {
												uni.hideLoading();
												this.datas.goods = goods
												this.valnum = 0
												uni.setStorageSync(
													'taobaoparamsdata', JSON
													.stringify(this.datas));
												uni.navigateTo({
													url: '/pages/taobaoimport/taobaogoods'
												});
											}
										}
									} else {
										uni.hideLoading();
										this.datas.goods = goods
										this.valnum = 0
										uni.setStorageSync(
											'taobaoparamsdata', JSON
											.stringify(this.datas));
										uni.navigateTo({
											url: '/pages/taobaoimport/taobaogoods'
										});
									}
								},
								fail: err => {
									console.error('请求失败：', err);
								}
							});
						} else if (e.url.includes("trade.tmall.com/detail/orderDetail.htm") || e.url
							.includes(
								"buyertrade.taobao.com/trade/detail/trade_item_detail.htm")) {
							uni.request({
								url: e.url,
								method: e.method,
								success: res => {
									if (this.timerId != null) {
										clearInterval(this.timerId);
									}
									uni.showLoading({
										title: `获取订单信息中 ${this.goodsindex + 1}/${this.datas.goods.length}`
									});
									let rawData = JSON.stringify(res)
									let trimmedString = rawData.replace(/\s/g, '');
									const regex = /vardetailData=(.*?)\\n<\/script>/;
									const match = trimmedString.match(regex);
									if (match) {
										const jsonString = match[1];
										let jsonString2 = jsonString.replace(/'/g, '');
										var stringWithoutEscape = jsonString2.replace(/\\/g,
											'');
										// console.log(stringWithoutEscape)
										let jsonstringWithoutEscape = JSON.parse(
											stringWithoutEscape)

										let prompt = jsonstringWithoutEscape.overStatus.prompt
										if (prompt && prompt.length > 0) {
											for (var i = 0; i < prompt.length; i++) {
												let item = prompt[i]
												if (item.key == '物流' || item.key == '配送时间') {
													this.datas.goods[this.goodsindex]
														.logisticscompany[0] = item.content[0]
														.mailNo ? item.content[0]
														.companyName : item.content[0]
														.companyName
													this.datas.goods[this.goodsindex]
														.logistics_sn[0] = item.content[0]
														.mailNo ? item.content[0]
														.mailNo : item.content[1]
														.mailNo
												}
											}

										} else {
											let list = jsonstringWithoutEscape.orders
												.list
											let goodnames = this.datas.goods[this.goodsindex]
												.good_name
											if (list && list.length > 0) {
												for (var i = 0; i < list.length; i++) {
													let logistic = list[i].logistic
													let status = list[i].status
													let title = status[0].subOrders[0].itemInfo
														.title
													for (var p = 0; p < goodnames
														.length; p++) {
														let itemgood = goodnames[p]
														if (itemgood == title) {
															this.datas.goods[this.goodsindex]
																.logisticscompany[p] = logistic
																.content[0].companyName
															this.datas.goods[this.goodsindex]
																.logistics_sn[p] = logistic
																.content[0].mailNo
														}
													}
												}

											}
										}


										if (this.goodsindex + 1 < this.datas.goods.length) {
											this.goodsindex += 1
											let url = this.datas.goods[this.goodsindex]
												.gooddetailurl
											wv.loadURL(url);
										} else {
											uni.showLoading({
												title: `订单信息校验中...`
											});
											let goods = this.fordatagoods()
											this.goodsdata = goods

											if (goods.length > 0) {
												for (var i = 0; i < goods.length; i++) {
													this.goodsindex = i
													let item = goods[i]
													if (item.logistics_sn == '0000') {
														let url =
															`https://detail.i56.taobao.com/trace/trace_detail.htm?tId=${item.order_sn}&userId=${item.sellerid}`
														wv.loadURL(url);
														return
													}

													if (this.goodsindex + 1 == goods
														.length) {
														uni.hideLoading();
														this.datas.goods = goods
														this.valnum = 0
														uni.setStorageSync(
															'taobaoparamsdata', JSON
															.stringify(this.datas));
														uni.navigateTo({
															url: '/pages/taobaoimport/taobaogoods'
														});
													}
												}

											} else {
												uni.hideLoading();
												this.datas.goods = goods
												this.valnum = 0
												uni.setStorageSync('taobaoparamsdata', JSON
													.stringify(this.datas));
												uni.navigateTo({
													url: '/pages/taobaoimport/taobaogoods'
												});
											}
										}
									} else {
										// console.log("未找到匹配的内容")
										const regex2 = /vardata=JSON.parse\(\'(.*?)\'\);/;
										const match2 = trimmedString.match(regex2);

										if (match2) {

											const jsonString = match2[1];
											var stringWithoutEscape = jsonString.replace(
												/\\\\\\\\\\\"|\\\\"/g,
												function(matched) {
													return matched === '\\\\\\\\\\\"' ?
														'\\\"' : '\"';
												});
											var stringWithoutEscape2 = stringWithoutEscape
												.replace(/(?!\\u|\\\/|\\\\\")\\/g, '');
											let jsonString2 = JSON.parse(stringWithoutEscape2)

											if (jsonString2.logisticsName) {
												this.datas.goods[this.goodsindex]
													.logisticscompany[0] = jsonString2
													.logisticsName
												this.datas.goods[this.goodsindex]
													.logistics_sn[0] = jsonString2.logisticsNum
											} else if (jsonString2.deliveryInfo) {
												this.datas.goods[this.goodsindex]
													.logisticscompany[0] = jsonString2
													.deliveryInfo.logisticsName
												this.datas.goods[this.goodsindex]
													.logistics_sn[0] = jsonString2.deliveryInfo
													.logisticsNum
											}


											if (this.goodsindex + 1 < this.datas.goods
												.length) {
												this.goodsindex += 1
												let url = this.datas.goods[this.goodsindex]
													.gooddetailurl
												wv.loadURL(url);
											} else {
												let goods = this.fordatagoods()
												this.goodsdata = goods

												if (goods.length > 0) {
													for (var i = 0; i < goods.length; i++) {
														this.goodsindex = i
														let item = goods[i]
														if (item.logistics_sn == '0000') {
															let url =
																`https://detail.i56.taobao.com/trace/trace_detail.htm?tId=${item.order_sn}&userId=${item.sellerid}`
															wv.loadURL(url);
															return
														}

														if (this.goodsindex + 1 == goods
															.length) {
															uni.hideLoading();
															this.valnum = 0
															this.datas.goods = goods
															uni.setStorageSync(
																'taobaoparamsdata', JSON
																.stringify(this.datas));
															uni.navigateTo({
																url: '/pages/taobaoimport/taobaogoods'
															});
														}
													}

												} else {
													uni.hideLoading();
													this.valnum = 0
													this.datas.goods = goods
													uni.setStorageSync('taobaoparamsdata', JSON
														.stringify(this.datas));
													uni.navigateTo({
														url: '/pages/taobaoimport/taobaogoods'
													});
												}
											}
										} else {
											console.log("未找到匹配的内容")
										}


									}
								},
								fail: err => {
									console.error('请求失败：', err);
								}
							});
						} else if (e.url.includes(
								"buyertrade.taobao.com/trade/itemlist/list_bought_items.htm")) {
							uni.showLoading({
								title: '加载中，耐心等待...'
							});
							let newurl = e.url + '&order_index=80'
							uni.request({
								url: e.url,
								method: e.method,
								success: res => {
									if (this.timerId != null) {
										clearInterval(this.timerId);
									}



									let rawData = JSON.stringify(res)
									this.loading = true
									const regex = /JSON\.parse(.*?)\;\\n\<\/script/;
									const match = rawData.match(regex);
									if (match) {
										const jsonString = match[1];
										let jsonString2 = jsonString.substring(1, jsonString
											.length - 1)
										let jsonString3 = jsonString2.replace(/'/g, '');
										var stringWithoutEscape = jsonString3.replace(/\\/g,
											'');

										let jsondatas = JSON.parse(stringWithoutEscape)
										let mainOrders = jsondatas.mainOrders
										let goods = []
										// console.log(mainOrders.length)

										if (mainOrders.length > 0) {
											for (var i = 0; i < mainOrders.length; i++) {
												let item = mainOrders[i]
												let subOrders = item.subOrders
												let b = {
													order_sn: item.id, //物品单号
													sellerid: item.seller.id,
													good_name: [], //物品名称
													good_price: [], //物品原来价格
													gooddetailurl: this.operationsdata(item
														.statusInfo.operations),
													// gooddetailurl: 'https:' + item
													// 	.statusInfo
													// 	.operations[0].url == undefined ? item
													// 	.statusInfo
													// 	.operations[1].url : item
													// 	.statusInfo
													// 	.operations[0].url,
													unit_price: [], //物品实付价格
													good_img: [], //物品图片
													logisticscompany: [], //物流公司
													logistics_sn: [], //物流单号
													good_Items: [], //物品品名
													num: [], //物品数量
													checked: false
												}

												for (var j = 0; j < subOrders.length; j++) {
													let itemj = subOrders[j]
													if (itemj.id) {
														let picimg = itemj.itemInfo.pic
															.replace('80x80', '300x300');
														let skuname = itemj.itemInfo
															.skuText
															.length > 0 ? this.decodeString(
																itemj
																.itemInfo.skuText[0].name) +
															'：' + this
															.decodeString(itemj
																.itemInfo
																.skuText[0].value) : ''
														b.good_name.push(this.decodeString(
															itemj
															.itemInfo.title))
														b.good_price.push(itemj
															.priceInfo
															.original ? itemj
															.priceInfo
															.original : itemj
															.priceInfo
															.realTotal)
														b.unit_price.push(itemj
															.priceInfo
															.realTotal)
														b.good_img.push('https:' + picimg)
														b.good_Items.push(skuname)
														b.num.push(itemj.quantity)
													}
												}
												this.datas.goods.push(b)
											}
											this.datas.user_id = this.data.user.user_id
											this.pageNum += 1
											wv.loadURL(
												`https://buyertrade.taobao.com/trade/itemlist/list_bought_items.htm?action=itemlist/BoughtQueryAction&event_submit_do_query=1&tabCode=waitConfirm&pageNum=${this.pageNum}`
											);
										} else {
											if (this.datas.goods.length > 0) {
												this.pageNum = 1
												this.goodsindex = 0
												let url = this.datas.goods[this.goodsindex]
													.gooddetailurl
												// console.log(JSON.stringify(this.datas.goods))
												if (url != '') {
													wv.loadURL(url);
												} else {
													wv.loadURL(url);
												}
												// wv.loadURL(url);
											} else {
												uni.hideLoading();
												this.datas.goods = []
												this.valnum = 0
												uni.setStorageSync('taobaoparamsdata', JSON
													.stringify(this.datas));
												uni.navigateTo({
													url: '/pages/taobaoimport/taobaogoods'
												});
											}


										}





										// console.log(JSON.stringify(this.datas))
										// this.pageNum += 1

										// this.goodsindex = 0
										// let url = this.datas.goods[this.goodsindex]
										// 	.gooddetailurl
										// wv.loadURL(url);
									} else {
										console.log("未找到匹配的内容")
										uni.hideLoading();
										uni.setStorageSync('taobaotesthtml', res.data);
										uni.navigateTo({
											url: '/pages/taobaoimport/taobaotest'
										});
									}
								},
								fail: err => {
									console.error('请求失败：', err);
									uni.hideLoading();
									uni.setStorageSync('taobaotesthtml', res.data);
									uni.navigateTo({
										url: '/pages/taobaoimport/taobaotest'
									});
									this.loading = false
								}
							});
						} else {
							if (e.url.includes("pass.tmall.com/add")) {
								if (this.valnum == 0) {
									this.valnum += 1
									wv.loadURL(
										`https://buyertrade.taobao.com/trade/itemlist/list_bought_items.htm?action=itemlist/BoughtQueryAction&event_submit_do_query=1&tabCode=waitConfirm&pageNum=${this.pageNum}`
									)
								} else {
									if (!this.isModalOpen) {
										this.openModal()
									}
									clearInterval(this.timerId);
									setTimeout(() => {
										console.log('重新加载')
										this.testurl.push(e.url)
										if (this.statusnum == 1) {
											this.createWebviewAndLoadURL2(e.url)
										}
									}, 2000);
									uni.off('loaded', wv);
								}

							} else {
								if (!this.isModalOpen) {
									this.openModal()
								}
								clearInterval(this.timerId);
								setTimeout(() => {
									console.log('重新加载')
									this.testurl.push(e.url)
									if (this.statusnum == 1) {
										this.createWebviewAndLoadURL2(e.url)
									}
								}, 2000);
								uni.off('loaded', wv);
							}
						}


					});

				})
			},

			operationsdata(operationss) {
				for (var i = 0; i < operationss.length; i++) {
					let item = operationss[i]
					// console.log(JSON.stringify(item))
					if (this.decodeString(item.text) == '订单详情') {
						let url = 'https:' + item.url
						return url
					}

					if (i + 1 == operationss.length) {
						return ''
					}
				}
			},
			openModal() {
				uni.showModal({
					title: '即将进入淘宝验证...（请耐心等待）',
					content: '注意：如提示验证失败，请返回首页重新登录。',
					success: function(res) {
						if (res.confirm) {
							// console.log('用户点击确定');
						} else if (res.cancel) {
							// console.log('用户点击取消');
						}
						// 关闭模态框时设置状态为false
						this.isModalOpen = false;
					}
				});
				// 打开模态框时设置状态为true
				this.isModalOpen = true;
			},


		},
		mounted() {
			this.valnum = 0
			this.statusnum = 1

			// setTimeout(() => {
			// 	// console.log(JSON.stringify(this.testurl))
			// 	uni.hideLoading();
			// 	let testurl = JSON.stringify(this.testurl)
			// 	uni.setStorageSync('taobaotesthtml', testurl);
			// 	uni.navigateTo({
			// 		url: '/pages/taobaoimport/taobaotest'
			// 	});
			// }, 30000)
		}
	}
</script>

<style>
	.uni-modal__bd {
		color: red !important;
		font-size: 13px !important;
	}
</style>